package com.timguan.render.dataprocessor.processor;

import com.timguan.render.api.entity.cms.CMSDataProcessorParamInfo;
import com.timguan.render.dataapi.api.ICmsDataManager;
import com.timguan.render.dataapi.domain.CmsDataDO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品轮播
 */
public class SilderProcessor extends DataProcessor {
    private static final Logger logger = LoggerFactory.getLogger(SilderProcessor.class);
    @Autowired
    private ICmsDataManager cmsDataManagerImpl;

    protected List<Object> execute(CommonParametersWrapper wrapper, Map<String, String> params) {
        if (params != null && params.get(PROCESSOR_PARAM_DATALISTID) != null) {
            final int listId = Integer.valueOf(params.get(PROCESSOR_PARAM_DATALISTID));
            final int count = StringUtils.isNotBlank(params.get(MAX_COUNT_PARAM)) && StringUtils.isNumeric(params.get(MAX_COUNT_PARAM)) ?
                    Integer.parseInt(params.get(MAX_COUNT_PARAM)) :
                    4;
            final int timeInterval = StringUtils.isNotBlank(params.get(PRODUCT_TIME_INTERVAL)) ?
                    Integer.parseInt(params.get(PRODUCT_TIME_INTERVAL)) :
                    3600000;
            List<CmsDataDO> cmsDataDOs = cmsDataManagerImpl.selectByListId(listId);
            int dataSize = 0;
            if (cmsDataDOs != null && cmsDataDOs.size() > 0) {
                dataSize = cmsDataDOs.size();
            }
            if (dataSize == 0) {
                return null;
            }
            if (count >= dataSize) {
                return appendSpm(wrapper, cmsDataDOs, Constants.SPM_START);
            }
            int group = getGroup(dataSize, count, timeInterval);
            return appendSpm(wrapper, getMatchedData(cmsDataDOs, dataSize, group, count), Constants.SPM_START);
        }
        return null;
    }

    //根据数组size，每组的个数，以及时间间隔来获取分组
    private int getGroup(int size, int count, int interVal) {
        if (size > 0 && count > 0 && interVal > 0) {
            //对数据进行分组
            int groupSize = (int) Math.ceil(size / count);
            return (int) ((System.currentTimeMillis() / 1000 / interVal) % groupSize);
        }
        return 0;
    }

    //找出count个商品
    private List<CmsDataDO> getMatchedData(List<CmsDataDO> cmsDataDOs, int size, int group, int count) {
        int index = group * count;
        List<CmsDataDO> dataDOs = new ArrayList<CmsDataDO>(count);
        int i = 0;
        int currentSize = 0;
        for (i = 0; i < size; index++, i++) {
            index = index % size;
            dataDOs.add(cmsDataDOs.get(index));
            currentSize += 1;
            if (currentSize == count) {
                break;
            }
        }
        return dataDOs;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //使用时需要创建数据列表
        needCreateDatalistWhenUsed = true;
        description = "随机轮换";
        paramInfos = new CMSDataProcessorParamInfo[2];
        CMSDataProcessorParamInfo dataProcessorParamInfo = null;
        dataProcessorParamInfo = new CMSDataProcessorParamInfo();
        dataProcessorParamInfo.setName(MAX_COUNT_PARAM);
        dataProcessorParamInfo.setDescription("展示数量,默认4");
        paramInfos[0] = dataProcessorParamInfo;
        dataProcessorParamInfo = new CMSDataProcessorParamInfo();
        dataProcessorParamInfo.setName(PRODUCT_TIME_INTERVAL);
        dataProcessorParamInfo.setDescription("时间间隔,单位秒");
        paramInfos[1] = dataProcessorParamInfo;
    }
}
